<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" type="text/css"
	href="../EXT4/resources/css/ext-all.css" />
<link rel="stylesheet" type="text/css"
	href="../css/style.css" />
<script type="text/javascript" src="../EXT4/ext-all.js"></script>
<script type="text/javascript" src="../script/ext-lang-zh_CN.js"></script>
<script type="text/javascript">
Ext.require([
    'Ext.form.field.ComboBox',
    'Ext.form.FieldSet',
    'Ext.tip.QuickTipManager',
    'Ext.data.*'
]);
Ext.onReady(function() {

	Ext.QuickTips.init();

	var classid = 0;
	var itemsPerPage = 20;
    //定义model 
    Ext.define('Class', { 
        extend: 'Ext.data.Model', 
        // fields定义字段和数据类型 
        fields: ['id', 'className', 'bishopName','assistantName','count','age'] 
    });
    
    Ext.define('Week',{
    	extend: 'Ext.data.Model',
    	fields: ['id','week']
    });
    
    Ext.define('Worktime',{
    	extend: 'Ext.data.Model',
    	fields: ['id','worktime']
    })
    
    var weekstates=[{'id':'1','week':'W1（第一周）'},
                    {'id':'2','week':'W2（第二周）'},
                    {'id':'3','week':'W3（第三周）'},
                    {'id':'4','week':'W4（第四周）'},
                    {'id':'5','week':'W5（第五周）'},
                    {'id':'6','week':'W6（第六周）'},
                    {'id':'7','week':'W7（第七周）'},
                    {'id':'8','week':'W8（第八周）'},
                    {'id':'9','week':'W9（第九周）'},
                    {'id':'10','week':'W10（第十周）'},
                    {'id':'11','week':'W11（第十一周）'},
                    {'id':'12','week':'W12（第十二周）'}];
    
    Ext.define('Student', { 
        extend: 'Ext.data.Model', 
        fields: ['id','displayName','school',
        'grade','age','mother','father','address','phone',
        'oaSubjects',{
        	name: 'sex',
        	convert: function(v, record) {
				if(v==1) return "男";
				if(v==0) return "女";
        	}
        },{
        	name: 'birthDate',
        	convert: function(v, record) {
        		var date = new Date(v);
        		return v.substring(0,10);
        	}
        },{
        	name: 'createTime',
        	convert: function(v, record) {
        		var date = new Date(v);
        		return v.substring(0,10);
        	}
        }] 
    });
    
    var classstore = Ext.create('Ext.data.Store', {
        model: 'Class', 
        autoLoad: true,
        pageSize: itemsPerPage,
        //使用ajax代理 
        proxy: { 
            type: 'ajax', 
            url: '../service/signin_findclass',
            //Reader 解码json数据 
            reader: { 
                type: 'json', 
                root: 'classvos', 
                total: 'total'
            }
        }
    });
    
	var s_select = Ext.create('Ext.selection.CheckboxModel');
    
    var studentstore = Ext.create('Ext.data.Store', {
        model: 'Student', 
        pageSize: 10,
        //使用ajax代理 
        proxy: { 
            type: 'ajax', 
            url: '../service/class1_findselected',
            //Reader 解码json数据 
            reader: { 
                type: 'json', 
                root: 'students'
            }
        }
    });
    
    var weekstore=Ext.create('Ext.data.Store', {
        autoDestroy: true,
        model: 'Week',
        data: weekstates
    });
    
    var worktimestore=Ext.create('Ext.data.Store', {
    	model: 'Worktime',
    	proxy: { 
            type: 'ajax', 
            pageSize: 10,
            url: '../service/class1_findworktime',
            //Reader 解码json数据 
            reader: { 
                type: 'json', 
                root: 'worktimes'
            }
        }
    });
    
    var sign_form = Ext.create('Ext.Panel', {
	    border: 0,
	    renderTo: 'sign',
	    layout: {
	        align: 'stretch'
	    },
        items: [{
	        xtype:'fieldset',
	        title: '考勤条件',
	        collapsible: true,
	        defaultType: 'textfield',
	        layout: 'hbox',
	        items :[Ext.create('Ext.form.field.ComboBox', {
        	    displayField: 'className',
        	    store: classstore,
        	    fieldLabel:'选择班级',
        	    valueField: 'id',
        	    id: 'classid',
        	    labelWidth:60,
                width:200,
                margin:'0 10 0 0',
                allowBlank: false,
        	    listeners: {
        	    	select: function(comb, records) {
        	    		Ext.each(records, function(record) {
        	    			classid = record.data.id;
        	    			worktimestore.load({
                    			params: {
                    		        id: record.data.id
                    		    },
                    		});
                		    Ext.getCmp('worktime').setDisabled(false);
                		    Ext.getCmp('worktime').setValue();
        	    		});
        	    	}
        	    }
        	}),Ext.create('Ext.form.field.ComboBox', {
        	    displayField: 'worktime',
        	    store: worktimestore,
        	    fieldLabel:'上课时间',
        	    valueField: 'id',
                queryMode: 'local',
	        	disabled: true,
                labelWidth:60,
                width:200,
                margin:'0 10 0 0',
        	    id: 'worktime',
                allowBlank: false,
        	    listeners: {
        	    	select: function(comb, records) {
        	    		Ext.each(records, function(record) {
        	    			Ext.getCmp('week').setDisabled(false);
        	    			Ext.getCmp('week').setValue();
        	    		});
        	    	}
        	    }
        	}),Ext.create('Ext.form.field.ComboBox', {
        	    displayField: 'week',
        	    store: weekstore,
        	    fieldLabel:'周数',
        	    valueField: 'id',
                queryMode: 'local',
	        	disabled: true,
                labelWidth:30,
                width:200,
                margin:'0 10 0 0',
        	    id: 'week',
                allowBlank: false,
        	    listeners: {
        	    	select: function(comb, records) {
        	    		Ext.each(records, function(record) {
        	    			Ext.Ajax.request({
        	        		    url: '../service/signin_getissign',
        	        		    params: {
        	        		        id: classid,
        	        		        week: record.data.id,
        	        		        worktime: Ext.getCmp('worktime').getValue()
        	        		    },
        	        		    success: function(response, opts) {
        	        		        var obj = Ext.decode(response.responseText);
        	        		        studentstore.removeAll();
        	        		        if(!obj.issign>0)
       	        		        	{
        	        		        	studentstore.load({
                    	    				params: {
                    	    					id: classid
                    	    				}
                    	    			});
       	        		        	}
        	        		        else
        	        		        {
        	        		        	Ext.Msg.alert('提示', "本周已考勤");
        	        		        }
        	        		    },
        	        		});
        	    		});
        	    	}
        	    }
        	})]
	    },{
        	xtype: 'grid',
	        selModel: s_select,
	        valueField: 'id',
	        id: 'student_grid',
	        columns:[{ 
	        	text: '学生姓名',  dataIndex: 'displayName', width:200
	        },{ 
	        	text: '联系方式',  dataIndex: 'phone', width:200
	        }],
	        store: studentstore,
	        tbar:[{
	            text: '保存',
	            handler: function() {
	            	var studentlist="";
					Ext.each(s_select.getSelection(),function(record, index) {
						studentlist+=record.data.id+",";
					});
					if(studentstore.getCount()>0) {
						Ext.Ajax.request({  
		     		        url : '../service/signin_add',  
		     		        method : 'POST',  
		     		        params : {
		     		        	week: Ext.getCmp('week').getValue(),
		     		        	classid: Ext.getCmp('classid').getValue(),
		     		        	studentlist:studentlist,
		     		        	worktime: Ext.getCmp('worktime').getValue()
		     		        },
		     		       	success:function(){//加载成功的处理函数  
			                   	Ext.Msg.alert('提示', "考勤成功");
		              		}
		     			});
					}
	        	}
	        }]
        }]
    });
});
</script>
</head>
<body>
<div id="sign"></div>
</body>
</html>